package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"role-mgr1/cache"
	"role-mgr1/models"
	"strconv"
	"time"
)

func RepoAdd(c *gin.Context) {
	repoName := c.PostForm("repoName")
	repoCode := c.PostForm("repoCode")
	if repoCode == "" || repoName == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	data := models.Repo{
		RepoName: repoName,
		RepoCode: repoCode,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func RepoOrderAdd(c *gin.Context) {
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	orderCode := c.PostForm("orderCode")
	orderForm := c.PostForm("orderForm")
	orderNum, _ := strconv.Atoi(c.PostForm("orderNum"))
	orderPrice, _ := strconv.Atoi(c.PostForm("orderPrice"))
	if rid == 0 || orderCode == "" || orderForm == "" || orderNum == 0 || orderPrice == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	data := models.RepoOrder{
		Rid:        rid,
		OrderCode:  orderCode,
		OrderNum:   orderNum,
		OrderPrice: orderPrice,
		OrderForm:  orderForm,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func RepoOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "5"))
	offset := (page - 1) * size
	var data []models.RepoOrderList
	/*
		SELECT
		repo_orders.id AS OrderId,
		repos.repo_name AS RepoName,
		repos.repo_code AS RepoCode,
		repo_orders.order_code AS OrderCode,
		repo_orders.order_num AS OrderNum,
		repo_orders.order_form AS OrderForm,
		repo_orders.order_price AS OrderPrice
		FROM
		  repo_orders
		  LEFT JOIN repos ON repo_orders.rid = repos.id
	*/
	redis_key := "repo_list" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("repo_orders").Select(
			"repo_orders.id AS OrderId",
			"repos.repo_name AS RepoName",
			"repos.repo_code AS RepoCode",
			"repo_orders.order_code AS OrderCode",
			"repo_orders.order_num AS OrderNum",
			"repo_orders.order_form AS OrderForm",
			"repo_orders.order_price AS OrderPrice",
		).Joins("LEFT JOIN repos ON repo_orders.rid = repos.id").
			Offset(offset).
			Limit(size).Find(&data)
		jsonData, _ := json.Marshal(data)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &data)
	}

	var count int64
	models.DB.Model(&models.RepoOrder{}).Count(&count)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"count": count,
		"data":  data,
	})
	return
}
func RepoOrderDetail(c *gin.Context) {
	id := c.Query("id")
	var data models.RepoOrderList
	/*
			SELECT
			repo_orders.id AS OrderId,
			repos.repo_name AS RepoName,
			repos.repo_code AS RepoCode,
			repo_orders.order_code AS OrderCode,
			repo_orders.order_num AS OrderNum,
			repo_orders.order_form AS OrderForm,
			repo_orders.order_price AS OrderPrice
			FROM
			  repo_orders
			  LEFT JOIN repos ON repo_orders.rid = repos.id
		 WHERE repo_orders.id = 1
	*/
	redis_key := "repo_detail" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("repo_orders").Select(
			"repo_orders.id AS OrderId",
			"repos.repo_name AS RepoName",
			"repos.repo_code AS RepoCode",
			"repo_orders.order_code AS OrderCode",
			"repo_orders.order_num AS OrderNum",
			"repo_orders.order_form AS OrderForm",
			"repo_orders.order_price AS OrderPrice",
		).Joins("LEFT JOIN repos ON repo_orders.rid = repos.id").
			Where("repo_orders.id = ?", id).Find(&data)
		jsonData, _ := json.Marshal(data)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &data)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": data,
	})
	return
}
